package com.xcf.controller.base;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author WongBin
 * @date 2017/11/2.
 */
@Controller
@RequestMapping("/common/export/")
public class FileExportController extends ExportFileSupportController {

    @ResponseBody
    @RequestMapping(value = "exportExcel", method = RequestMethod.GET)
    public void exportExcel(HttpServletRequest req, @RequestParam String exportType, HttpServletResponse response) {
        try {
            List dataList = null;//new ArrayList();
            String module = ExportCommonService.templateHolder.get(exportType+"Module");
            dataList = getResultSet(req,module);
            Map<String, Object> beans = new HashMap<String, Object>();
            beans.put("entries", dataList);
            super.exportExcel(ExportCommonService.templateHolder.get(exportType),
                    ExportCommonService.templateHolder.get(exportType+"Name"), beans, response);

        } catch (Exception e) {
            logger.error("export-error:{}",exportType,e);
        }
    }

    private List getResultSet(HttpServletRequest request,String module) throws Exception {
        Map param = getParams(request);
        List ret = exportService.getResult(param,module);
        return ret;
    }

    @Autowired
    private ExportCommonService exportService;
}
